package com.jboost.modules.area.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jboost.common.system.vo.AreaVo;
import com.jboost.common.util.RedisUtil;
import com.jboost.modules.area.constant.JboostAreaConstant;
import com.jboost.modules.area.entity.JboostAddress;
import com.jboost.modules.area.entity.JboostArea;
import com.jboost.modules.area.mapper.JboostAreaMapper;
import com.jboost.modules.area.service.JboostAreaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Description: csc_category
 * @Author: cxn
 * @Date:   2021-05-11
 * @Version: V1.0
 */
@Service
public class JboostAreaServiceImpl extends ServiceImpl<JboostAreaMapper, JboostArea> implements JboostAreaService {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private JboostAreaMapper areaMapper;

    @Override
    public AreaVo getByCode(String code) {
        if(StringUtils.isEmpty(code))return null;
        List<JboostArea> areaList = null;
        Object obj = redisUtil.get(JboostAreaConstant.AREA_LIST_CACHE);
        if(null != obj ){
            if(obj instanceof List){
                areaList = (List<JboostArea>) obj;
            }
        }else {
            areaList = areaMapper.selectList(new LambdaQueryWrapper<>());
            redisUtil.set(JboostAreaConstant.AREA_LIST_CACHE,areaList);
        }
        if (null == areaList)return null;
        JboostArea area = null;
        try {
            area = areaList.stream().filter(a -> code.equals(a.getCode())).findFirst().get();
        }catch (NoSuchElementException e){
            return null;
        }
        AreaVo vo = new AreaVo();
        BeanUtils.copyProperties(area,vo);
        return vo;
    }


    @Override
    public JboostAddress createAddress(String areaId, String address) {
        if (StrUtil.isBlank(areaId)) {
            return null;
        }

        Map<String, AreaVo> map = getProvinceCityCountyByAreaId(areaId);
        AreaVo province = map.get(JboostAreaConstant.PROVINCE);
        AreaVo city = map.get(JboostAreaConstant.CITY);
        AreaVo county = map.get(JboostAreaConstant.COUNTY);

        JboostAddress sysAddress = new JboostAddress();
        sysAddress.setAddress(address);
        sysAddress.setAreaId(areaId);
        sysAddress.setCounty(county.getName());
        sysAddress.setCity(city.getName());
        sysAddress.setProvince(province.getName());
        return sysAddress;
    }


    public Map<String, AreaVo> getProvinceCityCountyByAreaId(String areaId) {
        if (StringUtils.isBlank(areaId)) {
            return null;
        }

        Map<String, AreaVo> map = new HashMap<>();

        // 区/县
        JboostArea countyArea = areaMapper.selectById(areaId);
        log.error("areaId="+areaId+";obj="+countyArea);
        if(null==countyArea){
            AreaVo vo = this.getByCode(areaId);
            BeanUtils.copyProperties(vo,countyArea);
            log.error("areaId="+areaId+";obj="+countyArea);
        }
        if(null == countyArea){
            return null;
        }
        AreaVo countyVo = new AreaVo();
        BeanUtils.copyProperties(countyArea,countyVo);
        map.put(JboostAreaConstant.COUNTY, countyVo);

        // 市
        JboostArea cityArea = areaMapper.selectOne(Wrappers.<JboostArea>lambdaQuery().eq(JboostArea::getCode, countyArea.getPCode()));
        AreaVo cityVo = new AreaVo();
        BeanUtils.copyProperties(cityArea,cityVo);
        map.put(JboostAreaConstant.CITY, cityVo);

        // 省
        JboostArea provinceArea = areaMapper.selectOne(Wrappers.<JboostArea>lambdaQuery().eq(JboostArea::getCode, cityArea.getPCode()));
        AreaVo provinceVo = new AreaVo();
        BeanUtils.copyProperties(provinceArea,provinceVo);
        map.put(JboostAreaConstant.PROVINCE, provinceVo);

        return map;
    }

    //根据区域id查询在省市区树形结构中定位的id集合 父 -> 子
    public List<String> getAddressIdList(String areaId) {
        LinkedList<String> list = new LinkedList<>();
        list.addFirst(areaId);

        LambdaQueryWrapper<JboostArea> queryWrapper = new LambdaQueryWrapper<>();
        JboostArea county = areaMapper.selectById(areaId);
        JboostArea city = null;
        if (null != county){
            queryWrapper.eq(JboostArea::getCode, county.getPCode());
            city = areaMapper.selectOne(queryWrapper);
            list.addFirst(city.getId());
            queryWrapper.clear();
        }else {
            list.addFirst(null);
        }

        if (null!=city){
            queryWrapper.eq(JboostArea::getCode, city.getPCode());
            JboostArea province = areaMapper.selectOne(queryWrapper);
            list.addFirst(province.getId());
        }else {
            list.addFirst(null);
        }
        return list;
    }

}

